import datetime
import os
import time

import pyautogui
import redis

from utils import getTextPosition, click, find_window_position_and_check_foreground,getWalletInfo, is_similar, scrollWalletHeight, saveWalletList, close

walletText = "拼手气红包"


def reStart(r):
    print("==============没找到")
    time.sleep(0.2)
    pyautogui.scroll(-800)
    #start(r)

def start(r):
    # 连接redis
    parse = r.get("parse") or b'0'
    print(f"是否许需要解析的判断111111111111：{parse}")
    if parse == "0":
        # 暂停解析
        print('暂停解析，睡眠5秒')
        time.sleep(5)
        start(r)
    else:
        # 0：不抢就等待20秒
        # 1： 抢不等待
        isRob = r.get("isRob") or '0'
        # 发现红包等待的时常
        stopSeconds = r.get("stopSeconds") or '15'
        window = find_window_position_and_check_foreground("QQ")
        windowX, windowY, windowWidth, windowHeight, isForeground = window
        if windowX > -1 and windowY > -1 and windowWidth > -1 and windowHeight > -1:
            if isForeground:
                walletInfo = getWalletInfo(windowX, windowY, windowWidth, windowHeight)
                windowInfo = getTextPosition(walletText, windowX, windowY, windowWidth, windowHeight, 'windowInfo')
                if windowInfo[0] > -1:
                    # 找到了红包
                    if isRob != "1":
                        print(f"不抢  等待：{stopSeconds}")
                        time.sleep(int(stopSeconds))
                        click(windowX + windowInfo[0], windowY + windowInfo[1])
                    else:
                        click(windowX + windowInfo[0], windowY + windowInfo[1])
                        close(walletInfo[0] + walletInfo[2] - 30, walletInfo[1] + 8, "-1", windowHeight)
                        time.sleep(0.2)
                        pyautogui.scroll(-800)
                        start(r)
                    detailInfo = getTextPosition("查看领取详情", walletInfo[0], walletInfo[1], walletInfo[2],
                                                 walletInfo[3], 'walletInfo')
                    click(walletInfo[0] + detailInfo[0] + int(detailInfo[2] / 2),
                          walletInfo[1] + detailInfo[1] + int(detailInfo[3] / 2))
                    listInfo = getTextPosition("发出的" + walletText, walletInfo[0], walletInfo[1], walletInfo[2],
                                               walletInfo[3], 'walletInfo1')
                    if listInfo[0] > -1:
                        folder = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
                        prev_image_path = saveWalletList(walletInfo[0], walletInfo[1], walletInfo[2], walletInfo[3],
                                                         folder, "1")
                        for i in range(2, 15):
                            scrollWalletHeight(i)
                            current_image_path = saveWalletList(walletInfo[0], walletInfo[1], walletInfo[2],
                                                                walletInfo[3], folder, i)
                            if current_image_path and is_similar(prev_image_path, current_image_path):
                                print("🚩 已经滚动到底部，停止截图")
                                os.remove(current_image_path)
                                break
                            else:
                                prev_image_path = current_image_path
                        close(walletInfo[0] + walletInfo[2] - 30, walletInfo[1] + 8, folder, windowHeight)
                        print("==============没找到")
                        reStart(r)
                    else:
                        reStart(r)
                else:
                    print("==============没找到")
                    reStart(r)
            else:
                print("请注意：窗口不在前台，暂停1分钟")
                time.sleep(60)
                # start(r)

if __name__ == "__main__":
    # 连接redis
    while True:
        r = redis.Redis(host='localhost', port=6379, decode_responses=True)
        parse = r.get("parse") or b'0'
        print(f"是否许需要解析的判断：{parse}")
        if parse == "0":
            # 暂停解析
            print('暂停解析，睡眠5秒')
            time.sleep(5)
        else:
            try:
                start(r)
            except Exception as e:
                print("error:", e)